package com.openstack4j.qx.rest;

import java.io.IOException;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.core.UriBuilder;

import com.sun.grizzly.http.SelectorThread;
import com.sun.jersey.api.container.grizzly.GrizzlyWebContainerFactory;

/**
 * 
 * 启动接口服务：接口程序main方法，运行程序的入口
 * 
 * @author liulin
 * @version 2016-12-1
 */
public class Main {
	/**
	 * 启动服务main方法
	 * 
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {

		System.out.println("参数：\nip\nport");
		System.out.println("---------------------------");

		// 初始化参数
		String url = "http://127.0.0.1/";
		int port = 8080;
		// 替换服务器http参数中的IP地址
		if (args.length > 0)
			url = url.replace("127.0.0.1", args[0]);
		// 替换服务器启动端口
		if (args.length > 0)
			port = Integer.parseInt(args[1]);

		URI uri = UriBuilder.fromUri(url).port(port).build();
		Map<String, String> initParams = new HashMap<String, String>();
		// rest接口类的包地址
		initParams.put("com.sun.jersey.config.property.packages", "com.openstack4j.qx.rest");
		System.out.println("启动服务...");
		SelectorThread threadSelector = GrizzlyWebContainerFactory.create(uri, initParams);
		System.out.println(String.format("WADL地址 = " + "%sapplication.wadl\n输入回车停止服务...", uri));
		System.in.read();
		threadSelector.stopEndpoint();
	}
}
